package com.emirates.spring.banzen2;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.opencv.android.JavaCameraView;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfInt;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.Size;
//import org.opencv.core.Size;
import android.os.Environment;
import android.util.AttributeSet;
import android.util.Log;



public class BanzenCameraView extends JavaCameraView {
	private final String TAG = "Banzen2::BanzenCameraView";

	public BanzenCameraView(Context context, AttributeSet attrs) {
		super(context, attrs);
	
	}
	
	public BanzenCameraView(Context context,int CameraID)
	{
		super(context,CameraID);
	}

    public List<String> getEffectList() {
        return mCamera.getParameters().getSupportedColorEffects();
    }

    public boolean isEffectSupported() {
        return (mCamera.getParameters().getColorEffect() != null);
    }

    public String getEffect() {
        return mCamera.getParameters().getColorEffect();
    }

    public void setEffect(String effect) {
        Camera.Parameters params = mCamera.getParameters();
        params.setColorEffect(effect);
        mCamera.setParameters(params);
    }

    public List<Size> getResolutionList() {
        return mCamera.getParameters().getSupportedPreviewSizes();
        
    }

    public void setResolution(Size resolution) {
        disconnectCamera();
        mMaxHeight = resolution.height;
        mMaxWidth = resolution.width;
        connectCamera(getWidth(), getHeight());
    }

    public Size getResolution() {
        return mCamera.getParameters().getPreviewSize();
    }

	
	public void takePicture(final String fileName) {
        Log.i(TAG, "Taking picture");

        PictureCallback callback = new PictureCallback() {

			@Override
			public void onPictureTaken(byte[] data, Camera camera) {

			Bitmap picture = BitmapFactory.decodeByteArray(data, 0,
					data.length);
				
			//	Bitmap picture=BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath()+"/input.jpg");
				
				Mat originalMat = new Mat();
				Utils.bitmapToMat(picture, originalMat);
				
				//BanzenImageProcessor.setMyText("");
				BanzenImageProcessor.getlicensePlate( originalMat);
				
				
				picture.recycle();

				mCamera.startPreview();

			}
			
		};
		
		AutoFocusCallback cb = new AutoFocusCallback() {
			
			@Override
			public void onAutoFocus(boolean success, Camera camera) {
				// TODO Auto-generated method stub
				Log.i("AF","Autofocus");
			}
		};
		
		mCamera.autoFocus(cb);        
		mCamera.takePicture(null, null, callback);
		
		
       
    }
	
	private String myText;
	
	public String getMyText() {
		return myText;
	}

	public void setMyText(String myText) {
		this.myText = myText;
	}

	private void saveMat(Mat inputMat,String name)
	{
		
      Bitmap picture = Bitmap.createBitmap((int)Math.ceil( inputMat.size().width), (int)Math.ceil( inputMat.size().height), Bitmap.Config.ARGB_8888);
       Utils.matToBitmap(inputMat, picture);
       
      try {
          FileOutputStream out = new FileOutputStream( getPah(name));
          picture.compress(Bitmap.CompressFormat.JPEG, 90, out);
          picture.recycle();
          
      } catch (Exception e) {
          e.printStackTrace();
      }
	}

	
	private String getPah(String name)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss-SSSS");
        String currentDateandTime = sdf.format(new Date());
        String fileName = Environment.getExternalStorageDirectory().getPath() +
                               "/Banzen/"+ name + currentDateandTime + ".jpg";
        
       
		return fileName;
	}
}
